/**
 * Created by lijtg on 2015/10/21.
 */
$(document).ready(function () {
    var main = {
        config: {
        },
        cache: {
            timer: null
        },
        startTimer: function () {
            var btn = $('#pr-btn-msgCode');
            var time = 60;
            btn.attr('disabled', 'disabled');
            btn.html('重新发送(' + time + 'S)');
            main.cache.timer = setInterval(function () {
                if(time < 0){
                    btn.attr('disabled', false);
                    btn.html('重新发送');
                    clearInterval(main.cache.timer);
                    return;
                }
                btn.html('重新发送(' + time-- + 'S)');
            }, 1000);
        },
        sendMsgCode: function (){
            var btn = $('#pr-btn-msgCode');
            if(btn.attr('disabled') == 'disabled') return;
            var phone = '';
            btn.attr('disabled', 'disabled');
            $.ajax({
                type: "POST",
                url: common.hostPath + "/api/sendMsgCode",
                data: {
                    phone: phone
                },
                dataType : 'json',
                success: function (data) {
                    if (data.errCode == "000000") {
                        main.startTimer();
                    }
                    else {
                        common.responseFail(data)
                    }
                },
                complete: function(){
                    //main.startTimer();
                    btn.attr('disabled', false);
                },
                error: common.responseError
            });
        },
        getAcctInfo: function (){
            $.ajax({
                type: "GET",
                url: common.hostPath + "/api/accountQuery",
                async:false,
                data: {
                    IDENTIFICATION_TYPE: '101',
                    IDENTIFICATION_NUMBER: main.cache.identification_number,
                    CUSTOMER_NAME: main.cache.acct_name,
                    BASE_ACCT_NO: main.cache.base_acct_no,
                    REQ_DATA:JSON.stringify(main.cache.req_data)
                },
                dataType : 'json',
                success: function (data) {
                    if (data.errCode == "000000") {
                    	main.cache.balance=data.result.BODY.BALANCE;
                    }
                    else {
                        common.responseFail(data)
                    }
                },
                complete: function(){

                },
                error: common.responseError
            });
        },
        getBindAcctInfo: function (){
            $.ajax({
                type: "GET",
                url: common.hostPath + "/api/bindAccountQuery",
                async:false,
                data: {
                    IDENTIFICATION_TYPE: '101',
                    IDENTIFICATION_NUMBER: main.cache.identification_number,
                    CUSTOMER_NAME: main.cache.acct_name,
                    BASE_ACCT_NO: main.cache.base_acct_no,
                    REQ_DATA:JSON.stringify(main.cache.req_data)
                },
                dataType : 'json',
                success: function (data) {
                    if (data.errCode == "000000") {
                    	main.cache.linked_base_acct_no=data.result.LINKED_ACCT_ARRAY[0].LINKED_BASE_ACCT_NO;
                    	main.cache.linked_bank_code=data.result.LINKED_ACCT_ARRAY[0].LINKED_BANK_CODE;
                    	main.cache.linked_bank_name=data.result.LINKED_ACCT_ARRAY[0].LINKED_BANK_NAME;
                    }
                    else {
                        common.responseFail(data)
                    }
                },
                complete: function(){

                },
                error: common.responseError
            });
        },
        submit: function (){
            var btn = $('#pr-btn-submit');
            if(btn.attr('disabled') == 'disabled') return;
            if(!main.formCheck()) return;

            var transPwd = $.trim($('#pr-transPwd').val());
            main.cache.tradeType = $('#trade-type').val();

            btn.attr('disabled', 'disabled');
            if(main.cache.tradeType == '0'){//T+0
                $.ajax({
                    type: "POST",
                    url: common.hostPath + "/api/accountOut",
                    data: {
                        CUSTOMER_NAME: main.cache.acct_name,
                        BASE_ACCT_NO: main.cache.base_acct_no,
                        OTH_BASE_ACCT_NO: main.cache.linked_base_acct_no,
                        OTH_BANK_CODE: main.cache.linked_bank_code,
                        OTH_BANK_NAME: main.cache.linked_bank_name,
                        TRADE_AMOUNT: main.cache.trade_amount,
                        THREE_REF_NO: main.cache.three_ref_no,
                        PASSWORD: depositEncrypt.encryptByDES(transPwd),
                        REQ_DATA:JSON.stringify(main.cache.req_data)
                    },
                    dataType : 'json',
                    success: function (data) {
                        if (data.errCode == "000000") {
                            layer.open({
                                content: '提现成功',
                                btn: '确定'
                            });
                        }
                        else {
                            common.responseFail(data)
                        }
                    },
                    complete: function(){
                        btn.attr('disabled', false);
                    },
                    error: common.responseError
                });
            }
            else{//T+1
                $.ajax({
                    type: "POST",
                    url: common.hostPath + "/api/withdrawDeposit",
                    data: {
                        BASE_ACCT_NO: '',
                        TRAN_TYPE: 'CM000004',
                        BUSINESS_TYPE: '00000011',
                        ACCT_DATE: '',
                        TRADE_AMOUNT: '',
                        FEE_MODE: ''
                    },
                    dataType : 'json',
                    success: function (data) {
                        if (data.errCode == "000000") {
                            layer.open({
                                content: '提现成功',
                                btn: '确定'
                            });
                        }
                        else {
                            common.responseFail(data)
                        }
                    },
                    complete: function(){
                        btn.attr('disabled', false);
                    },
                    error: common.responseError
                });
            }
        },
        formCheck: function () {
            var bank = $('#ex-bank').val();
            if (!bank) {
                common.alertLayer({
                    errMsg: ' 请选择提现银行卡'
                });
                return false;
            }

            var transPwd = $.trim($('#pr-transPwd').val());
            if (isNaN(transPwd) || transPwd.length != 6) {
                common.alertLayer({
                    errMsg: ' 支付密码有误'
                });
                return false;
            }

/*            var msgCode = $('#pr-msgCode').val();
            if($.trim(msgCode) == '') {
                common.alertLayer({
                    errMsg: '验证码有误'
                });
                return false;
            }*/

            return true;
        },
        regHammer: function () {
            var hammer2 = new Hammer($('#pr-btn-msgCode')[0]);
            hammer2.on('tap', function (ev) {
                main.sendMsgCode();
            });
            var hammer4 = new Hammer($('#pr-btn-submit')[0]);
            hammer4.on('tap', function (ev) {
                main.submit();
            });
        },
        initPara: function () {
        	$('#acct_name').html(main.cache.acct_name);
        	$('#three_ref_no').html(main.cache.three_ref_no);
        	$('#base_acct_no').html(main.cache.base_acct_no);
        	$('#trade_amount').html(main.cache.trade_amount);
        	$('#busi_plat_name').html(main.cache.busi_plat_name);
        	$('#balance').html(main.cache.balance);
        	$('#extract_amount').html(main.cache.trade_amount);

        },
        init: function () {
            $('#ex-bank').select2().on('change', function () {
            });
        	main.cache.three_ref_no = common.getUrlParams().three_ref_no;
        	main.cache.busi_plat_name = common.getUrlParams().busi_plat_name;
        	main.cache.busi_plat_code = common.getUrlParams().busi_plat_code;
        	main.cache.base_acct_no = common.getUrlParams().base_acct_no;
        	main.cache.identification_number = common.getUrlParams().identification_number;
        	main.cache.acct_name = common.getUrlParams().acct_name;
        	main.cache.trade_amount = common.getUrlParams().trade_amount;
        	main.cache.req_data = common.getUrlParams();
            main.regHammer();
            main.getAcctInfo();
            main.getBindAcctInfo();
            main.initPara();
        }
    };

    main.init();
});